package com.service;

import com.fh_23.bean.Student;
import com.fh_23.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class StudentTest {
    public static void main(String[] args) throws IOException {
//        加载核心配置文件
//        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
////        获取SqlSession工厂对象
//        SqlSessionFactory fy = new SqlSessionFactoryBuilder().build(is);
////        获取SqlSession对象
//        SqlSession sq = fy.openSession();
//        //底层创建了StudentMapper接口的子类对象
////        接口代理  这一行代码能够获取到student接口类型的对象
//        StudentMapper mp = sq.getMapper(StudentMapper.class);
//        Student s1 = new Student("s012", "大波浪", 18,"男");
//        mp.addStudent(s1);
//        sq.commit();
//        is.close();

//        查询
//        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//        SqlSessionFactory fy = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sq = fy.openSession();
//        StudentMapper mp = sq.getMapper(StudentMapper.class);
//        Student s = mp.selectStudent("s011");
//        System.out.println(s);

//        修改
//        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//        SqlSessionFactory fy = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sq = fy.openSession(true);
//        StudentMapper mp = sq.getMapper(StudentMapper.class);
//        int i = mp.updateStudent(new Student("s011", "小蛮腰", 19, "女"));
//        System.out.println(i);
//        is.close();

//        根据姓名查询学生
//        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//        SqlSessionFactory fy = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sq = fy.openSession(true);
//        StudentMapper mp = sq.getMapper(StudentMapper.class);
//        Student st = new Student();
////        st.setSname("");
//        st.setSage(21);
//        st.setSsex("女");
//        List<Student> students = mp.selectStu(st);
//        System.out.println(students);

//            根据多个id查询学生
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory fy = new SqlSessionFactoryBuilder().build(is);
        SqlSession sq = fy.openSession(true);
        StudentMapper mp = sq.getMapper(StudentMapper.class);
        ArrayList<String> list = new ArrayList<>();
        list.add("s001");
        list.add("s002");
        list.add("s003");
        list.add("s005");
        List<String> strings = mp.selectBySno(list);
        System.out.println(strings);
        is.close();
    }
}
